package org.brisling.mobile.service;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.common.ConstSysParam;
import org.brisling.medical.domain.MedicalInpatient;
import org.brisling.medical.queryService.QueryInpatientService;
import org.brisling.mobile.config.MDomain;
import org.brisling.mobile.config.RSAConfig;
import org.brisling.mobile.service.base.AbstractBaseMobileService;
import org.brisling.mobile.vo.satis.VOSubject;
import org.brisling.mobile.vo.sysman.VODept;
import org.brisling.satisfaction.queryService.QuerySatisfactionEvaluateResultService;
import org.brisling.sysman.queryService.QueryDeptService;
import org.brisling.sysman.queryService.QueryUploadService;
import org.brisling.sysman.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.codec.Base64;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 
 * <p>
 * Title: MedicalService
 * </p>
 * <p>
 * Description: 医疗风险服务类
 * </p>
 * <p>
 * Company: 长沙东勤信息科技发展有限公司
 * </p>
 * 
 * @author jackson wang
 * @since 1.0
 * @version 1.0
 * @date 2017年5月13日 下午4:06:19
 */
@RestController
@RequestMapping("/app/medical")
public class MedicalService extends AbstractBaseMobileService {

	@Autowired
	private UploadService uploadService;

	@Autowired
	private QueryUploadService qryUploadService;

	@Autowired
	private ShiroSecurityService shiroSecurityService;

	@Autowired
	private QueryInpatientService qryInpatientService;

	@Autowired
	private QuerySatisfactionEvaluateResultService qryserService;

	@Autowired
	private QueryDeptService qryDeptService;

	private static Map<String, VOSubject> problemMap = null;

	/**
	 * 
	 * 
	 * 
	 * @param path
	 *            路径
	 * @param keycode
	 *            密钥对键值代码
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getKey.m")
	public MDomain getKey(HttpServletResponse response) {
		MDomain m = new MDomain();

		String code = RSAConfig.getInitKey();
		m.setC(code);
		Map<PublicKey, PrivateKey> keyMap = RSAConfig.getVerifyKeyPaire(code);

		PublicKey key = RSAConfig.getPublicKey(keyMap);
		byte[] pb = key.getEncoded();
		String pbStr = new String(Base64.encode(pb));
		m.setP(pbStr);

		response.setHeader("Access-Control-Allow-Origin", "*");
		return m;
	}

	/**
	 * 获取版本文件
	 * 
	 * @param id
	 *            版本文件记录id
	 * @return
	 */
	@RequestMapping("/df.m")
	public void downloadFile(@RequestParam(value = "id", required = false) Long id, HttpServletRequest request,
			HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		uploadService.downloadFile(id, request, response);

	}

	@RequestMapping("/getCurrentPatient.m")
	public List<MedicalInpatient> getCurrentPatient(@RequestParam(value = "userid", required = false) String userid,
			@RequestParam(value = "deptid", required = false) String deptid, HttpServletResponse response) {

		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("userid", userid);
		map.put("deptid", deptid);

		return qryInpatientService.getCurrentPatient(map, ConstSysParam.QUERY_MEDICAL_ALL);
	}

	/**
	 * 获取当前用户管理的科室
	 * 
	 * @param userid
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getCurrentDept.m")
	public List<VODept> getCurrentDept(@RequestParam(value = "userid", required = false) String userid,

			HttpServletRequest request, HttpServletResponse response) {

		response.setHeader("Access-Control-Allow-Origin", "*");

		return qryDeptService.getCurrentUserDeptList(userid, ConstSysParam.QUERY_MEDICAL_ALL);
	}

}
